import Foundation
import Security

struct MastgTest {
    static func mastgTest(completion: @escaping (String) -> Void) {

	// Step 1: Use a hardcoded RSA private key (in DER format)
	let privateKeyBytes: [UInt8] = [
		0x30, 0x82, 0x02, 0x5b, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbd, 0xf6, 0x89, 0x8f, 0xbd, 
		0x0c, 0xe6, 0x4f, 0x9a, 0x97, 0xec, 0x30, 0x1a, 0x48, 0x93, 0x4d, 0x2a, 0xbf, 0xdf, 0xf7, 0x08, 
		0x15, 0x4c, 0xdb, 0x87, 0xe5, 0xdf, 0xb1, 0xcf, 0x8d, 0xa2, 0x5e, 0x2a, 0x7d, 0x92, 0xa8, 0xbd, 
		0x30, 0xb9, 0x10, 0xcf, 0x96, 0xda, 0x10, 0x25, 0xd4, 0x67, 0xaf, 0x92, 0x26, 0xfa, 0x43, 0xb7, 
		0x86, 0x83, 0xa2, 0x68, 0xd4, 0x7a, 0x56, 0xce, 0x41, 0x41, 0xed, 0xc0, 0x10, 0x0c, 0x89, 0xa5, 
		0x4e, 0x5f, 0xc4, 0x59, 0xcc, 0x55, 0x09, 0x1f, 0xa1, 0x1f, 0x1f, 0xb7, 0x97, 0x1d, 0x53, 0x1b, 
		0x73, 0x38, 0x4c, 0x3e, 0xe8, 0x28, 0x1d, 0xc1, 0x68, 0x6a, 0x8d, 0x3e, 0xb0, 0xf2, 0x0a, 0xb3, 
		0x74, 0x26, 0xd2, 0xdc, 0xcf, 0xa4, 0xac, 0xf3, 0x5b, 0x26, 0x35, 0xd1, 0x2d, 0xee, 0x91, 0x41, 
		0xa6, 0x93, 0xed, 0x91, 0xfa, 0xd7, 0x43, 0x83, 0x85, 0x53, 0xe5, 0x02, 0x03, 0x01, 0x00, 0x01, 
		0x02, 0x81, 0x80, 0x01, 0xed, 0xbf, 0x79, 0xcd, 0xee, 0x94, 0x32, 0x73, 0x1c, 0x5f, 0x5e, 0x88, 
		0x0f, 0x62, 0x94, 0x48, 0x98, 0x3b,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		0x3a, 0x45, 0x30, 0x4f, 0x73, 0x6a, 0xcb, 0x5b, 0x86, 0xcc, 0x00, 0x1f, 0x11, 0x02, 0x40, 0x5e, 
		0x6b, 0xae, 0x7c, 0x7d, 0xfc, 0x8a, 0xf5, 0x97, 0xa9, 0x39, 0x37, 0x27, 0x4e, 0xd9, 0xe8, 0xd0, 
		0x8d, 0xa9, 0x32, 0xe4, 0x6b, 0xde, 0x76, 0x0f, 0xee, 0xf1, 0x22, 0x11, 0xc4, 0x1e, 0x4a, 0x01, 
		0x73, 0xc4, 0x34, 0x8c, 0x4d, 0x16, 0xf1, 0x2f, 0x5f, 0xe4, 0xe5, 0x41, 0x3a, 0x3b, 0xf1, 0xb1, 
		0x78, 0x7c, 0x0e, 0x55, 0xf9, 0xf7, 0xcf, 0x64, 0x93, 0xad, 0x77, 0xfd, 0x31, 0x52, 0x87
	]
	let privateKeyData = Data(privateKeyBytes)

	let keyAttributes: [String: Any] = [
		kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
		kSecAttrKeyClass as String: kSecAttrKeyClassPrivate,
		kSecAttrKeySizeInBits as String: 1024,
		kSecReturnPersistentRef as String: true
	]

	var error: Unmanaged<CFError>?
	guard let privateKey = SecKeyCreateWithData(privateKeyData as CFData, keyAttributes as CFDictionary, &error) else {
		completion("Failed to create private key: \(String(describing: error))")
		return
	}

        guard let publicKey = SecKeyCopyPublicKey(privateKey) else {
            completion("Failed to generate public key")
            return
        }

        // Convert the public key to data (DER format)
        guard let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, &error) as Data? else {
            completion("Failed to extract public key: \(String(describing: error))")
            return
        }

        // Encode the public key for display
        let publicKeyHex = publicKeyData.map { String(format: "%02hhx", $0) }.joined()

        // Data to sign
        let dataToSign = "This is a sample text".data(using: .utf8)!

        // Step 2: Sign the data with the hardcoded private key
        guard let signature = SecKeyCreateSignature(
            privateKey,
            SecKeyAlgorithm.rsaSignatureMessagePKCS1v15SHA256,
            dataToSign as CFData,
            &error
        ) else {
            completion("Signing failed: \(String(describing: error))")
            return
        }

        // Convert signature to hex string for display
        let signatureHex = (signature as Data).map { String(format: "%02hhx", $0) }.joined()

        // Step 3: Verify the signature with the public key
        let verificationStatus = SecKeyVerifySignature(
            publicKey,
            SecKeyAlgorithm.rsaSignatureMessagePKCS1v15SHA256,
            dataToSign as CFData,
            signature as CFData,
            &error
        )

        let verificationResult = verificationStatus ? "Signature is valid." : "Signature is invalid."

        let value = """
        Original: \(String(data: dataToSign, encoding: .utf8)!)
        
        Public Key (Hex): \(publicKeyHex)
        
        Signature (Hex): \(signatureHex)
        
        Verification: \(verificationResult)
        """

        completion(value)
    }
}
